﻿using Gma.UserActivityMonitor;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ColorGrabber
{
    /// <summary>
    /// Uses Global Mouse and Keyboard Hooks lib to hook and monitor mouse move and click
    /// http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
    /// Captures pixel color at mouse position
    /// </summary>
    public partial class ColorGrabberFRM : Form
    {
        /// <summary>
        /// Variable that tracks if the application is in capture mode.
        /// When true, on next mouse click set capture values
        /// </summary>
        private bool runningCapture = false;

        #region Initialisation

        public ColorGrabberFRM()
        {
            InitializeComponent();
        }

        private void ColorGrabberFRM_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
            RegisterHookEvents();
        }

        #endregion End Initialisation

        #region Control Events

        private void HookManager_MouseMove(object sender, MouseEventArgs e)
        {
            UpdateColor();
        }

        private void BTN_Capture_Click(object sender, EventArgs e)
        {
            ToggleCapture();
        }

        private void ColorGrabberFRM_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnRegisterHookEvents();
        }

        private void BTN_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void ColorGrabberFRM_MouseEnter(object sender, EventArgs e)
        {
            this.Focus();
        }

        #endregion End Control Events

        private void HookManager_MouseClick(object sender, MouseEventArgs e)
        {
            if (runningCapture && e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                //HookManager.MouseDown += HookManager_MouseClick;
                runningCapture = false;
                BTN_Capture.BackColor = Color.Transparent;
                UpdateColor(true);
            }
        }

        private void RegisterHookEvents()
        {
            HookManager.MouseMove += HookManager_MouseMove;
        }

        private void UnRegisterHookEvents()
        {
            HookManager.MouseMove -= HookManager_MouseMove;
            HookManager.MouseDown -= HookManager_MouseClick;
        }

        /// <summary>
        /// Gets the Pixel color of the current position of the mouse.
        /// Updates the preview and text fields with the Hex and RGB values
        /// </summary>
        /// <param name="capture">If true also updates the capture values.</param>
        private void UpdateColor(bool capture = false)
        {
            //Get the color object
            Color previewColor = ColorGetter.GetPixelColor(Cursor.Position.X, Cursor.Position.Y);

            //Set the preview and preview values
            PNL_Preview.BackColor = previewColor;
            //Get the hex value
            TXT_PrevHex.Text = string.Format("#{0}{1}{2}",
                previewColor.R.ToString("X2"),
                previewColor.G.ToString("X2"),
                previewColor.B.ToString("X2"));
            //Get RGB
            TXT_PrevR.Text = previewColor.R.ToString();
            TXT_PrevG.Text = previewColor.G.ToString();
            TXT_PrevB.Text = previewColor.B.ToString();
            if (capture)
            {
                //If capturing and mouse click detected 
                //Set preview panel
                PNL_Cap.BackColor = previewColor;
                //Set Hex
                TXT_CapHex.Text = TXT_PrevHex.Text;
                //Set RGB
                TXT_CapR.Text = previewColor.R.ToString();
                TXT_CapG.Text = previewColor.G.ToString();
                TXT_CapB.Text = previewColor.B.ToString();
            }
        }

        /// <summary>
        /// Toggles the capture mode from on to off and visa-versa
        /// </summary>
        /// <param name="captureModeOn">When true, calls to this method will only toggle on not off.</param>
        internal void ToggleCapture(bool captureModeOn = false)
        {
            if (!captureModeOn || !runningCapture)
            {
                runningCapture = !runningCapture;
                if (runningCapture) HookManager.MouseDown += HookManager_MouseClick;
                else HookManager.MouseDown -= HookManager_MouseClick;
                BTN_Capture.BackColor = runningCapture ? Color.Orange : Color.Transparent;
            }
        }



    }
}
